<template>
  <el-row :gutter="10" class="cont">
    <el-col class="left-aside" :lg="5">
      <div class="classification">
        <div class="title">按分类</div>
        <el-row :gutter="10">
          <ul>
            <template v-for="(v,index) in category">
              <li class="classification-list" :key="index" >
                <el-button
                @click="choose(index)"
                  :class="activeId==v.category_id?'active':''"
                  type="text"
                >{{v.category_name}}</el-button>
              </li>
            </template>
          </ul>
        </el-row>
      </div>
      <div class="news">
        <template>
          <el-tabs v-model="activeName">
            <el-tab-pane label="新闻公告" name="first">
              <template>
                <el-carousel height="200px" direction="vertical" :autoplay="false" :interval="5000">
                  <el-carousel-item v-for="item in 3" :key="item">
                    <ul class="news-list">
                      <li v-for="item in prizeList" :key="item.news_id">{{item.news_title}}</li>
                    </ul>
                  </el-carousel-item>
                </el-carousel>
              </template>
            </el-tab-pane>
          </el-tabs>
        </template>
      </div>
      <div class="news pain">
        <template>
          <el-tabs v-model="activeName">
            <el-tab-pane label="资源统计" name="first">
              <div class="zy" :style="{ 'background':'url('+ bg +') no-repeat 0 0' }">
                <span>电子图书册数</span>
                <span>{{total}}</span>
              </div>
            </el-tab-pane>
          </el-tabs>
        </template>
      </div>
    </el-col>
    <el-col class="right-main" :lg="19">
      <p class="where">
        当前位置:
        <span>首页</span>
      </p>
      <template>
        <el-tabs v-model="activeName">
          <el-tab-pane label="全部图书" name="first">
            <div class="all tabs">
              <!-- <div class="tab">
                <ul class="tab">
                  <li class="active">全部</li>
                  <li>阅读量</li>
                  <li>评分</li>
                  <li>下载量</li>
                  <li>评论量</li>
                </ul>
              </div>-->
              <template>
                <el-tabs v-model="form.sort" type="card" @tab-click="handleClick">
                  <el-tab-pane label="全部" name="create_time"></el-tab-pane>
                  <el-tab-pane label="阅读量" name="clicks"></el-tab-pane>
                  <el-tab-pane label="评分" name="rates"></el-tab-pane>
                  <el-tab-pane label="下载量" name="downloads"></el-tab-pane>
                  <el-tab-pane label="评论量" name="comments"></el-tab-pane>
                </el-tabs>
              </template>
            </div>
          </el-tab-pane>
        </el-tabs>
      </template>
      <el-row class="list" v-if="datas.length!=0">
        <template  v-for="(k,index) in datas">
          <el-col :span="12" class="list-li" :key="index">
            <el-row>
              <el-col :span="8" @click.native="link(index)">
                <img :src="k.cover" alt />
              </el-col>
              <el-col :span="14">
                <p class="name">{{k.title}}</p>
                <p class="point">
                  <span>点赞：</span>
                  <el-rate v-model="k.rates" disabled show-score text-color="#ff9900"></el-rate>
                </p>
                <p class="info">
                  <span>简介:</span>
                  <el-button>{{k.description==''?'暂无简介':k.description}}</el-button>
                </p>
                <p class="icon">
                  <el-row>
                    <el-col :span="7"   :class="collects.includes(k.file_id)?state:''">
                      <i
                        @click="collections(index)"
                        class="el-icon-star-on"
                        :class="collects.includes(k.file_id)?state:''"
                      ></i>
                      {{collects.includes(k.file_id)?'已收藏':'收藏'}}
                    </el-col>
                    <el-col :span="7">
                      <img :src="liulan" alt />
                      {{k.clicks}}
                    </el-col>
                    <el-col :span="7" @click="pl(index)">
                      <i class="el-icon-chat-dot-round"></i>
                      {{k.comments}}
                    </el-col>
                  </el-row>
                </p>
              </el-col>
            </el-row>
          </el-col>
        </template>


        <el-col class="total">
          <el-pagination
            @current-change="handleCurrentChange"
            background
            layout="prev, pager, next"
            size="10"
            :total="total"
          ></el-pagination>
        </el-col>
      </el-row>
      <el-row v-else>
         <el-col>
              <p style="text-align:center;">暂无数据</p>
           </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import store from "../../store";
import { bookList, operation, removeFavorite } from "@/api/page";
import { getToken1 } from "@/utils/auth";
export default {
  name: "cont",
  data() {
    return {
      value: 5,
      fm: require("@/assets/index/fengmian.png"),
      collection: require("@/assets/index/collection.png"),
      liulan: require("@/assets/index/liulan.png"),
      pinglun: require("@/assets/index/pinglun.png"),
      bg: require("@/assets/index/bg_ziyuantongji.png"),
      activeName: "first",
      prizeList: [],
      token: getToken1,
      activeIndex: 0,
      active: "first",
      intnum: undefined,
      category: [], //分类数组
      activeId: null,
      datas: [],
      total: null,
      collects: [],
      form: {
        page: null,
        page_size: 10,
        search: "",
        category_id: null,
        sort: "create_time",
        order: "asc",
      },
      state: "state",
    };
  },
  created() {
    this.fentch();
    this.token = getToken1();
  },
  methods: {
    // 获取数据
    fentch() {
      this.listLoading = true;
      bookList().then((response) => {
        console.log(response);
        this.category = response.data.category;
        this.datas = response.data.data;
        console.log(this.datas.length)
        this.prizeList = response.data.news;
        this.collects = response.data.collection;
        this.total = response.total;
        this.listLoading = false;
      });
    },
    // 重新获取数据
    dataList() {
      this.listLoading = true;
      let params = this.form;
      bookList(params).then((response) => {
        this.datas = response.data.data;
        this.collects = response.data.collection;
          this.total = response.total;
        this.listLoading = false;
      });
    },
    // 验证权限
    role() {
      if (!this.token) {
        this.$confirm("额。。。，您好像没有权限，是否去登录?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$router.push("/login");
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "您继续浏览吧",
            });
            return false;
          });
      }
    },
    select(index) {
      this.active = index;
    },
    link(index) {
      this.$router.push({
                     path: '/page/library/reading',
                      query: {
                        id: this.datas[index].file_id
                      }
                    })
    },
    handleClick(tab, event) {
      this.form.sort = tab.name;
      this.dataList();
    },
    choose(index) {
      this.activeId=this.category[index].category_id
  this.form.category_id=this.category[index].category_id
      this.dataList()
    },
    // 操作收藏
    collections(index) {
      this.role();
      if (this.token) {
        if (this.collects.includes(this.datas[index].file_id)) {
          this.removeFavorites(index);
        } else {
          this.operations(index);
        }
      }
    },
    // 取消收藏
    removeFavorites(index) {
      let params = {
        id: this.datas[index].file_id,
        type: 1,
      };
      removeFavorite(params).then((response) => {
        if (response.code == 1) {
          this.$message({
            type: "success",
            message: "取消成功",
          });
          this.dataList();
        }
      });
    },
    //收藏
    operations(index) {
      let params = {
        id: this.datas[index].file_id,
        type: 1,
        classify: 1,
      };
      operation(params).then((response) => {
        if (response.code == 1) {
          this.$message({
            type: "success",
            message: "收藏成功",
          });
          this.dataList();
        }
      });
    },
    //  评论
    pl(index) {},
    // 分页
    handleCurrentChange(val) {
      this.form.page = val;
      this.dataList();
    },
  },
};
</script>
<style lang="scss" scoped>
.table-content {
  height: 570px;
  overflow: hidden;
}
.right-main {
  position: relative;
  padding-left: 25px !important;
}
.classification {
  background: rgba(89, 186, 189, 1);
  border-radius: 10px;
  text-align: center;
  padding: 20px;
  color: rgba(255, 255, 255, 1);
  ul {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
  }
  li {
    border-top: 1px solid rgba(132, 209, 212, 1);
    padding: 10px 0;
    border-width: 100%;
  }
  .el-button--text {
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: rgba(255, 255, 255, 1);
  }
}
.title {
  font-size: 26px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: rgba(255, 255, 255, 1);
  text-align: left;
  padding-bottom: 20px;
}
.active {
  color: #ffde00 !important;
}
.news {
  padding: 30px 0;
}
.where {
  position: absolute;
  right: 0;
  top: 0;
  font-size: 18px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: rgba(142, 142, 142, 1);
}
.tab {
  text-align: right;
  margin-top: 0px;
  border-right: 1px solid #e4e4e4;
  li {
    display: inline-block;
    padding: 12px 30px;
    border-left: 1px solid #e4e4e4;
    border-bottom: 1px solid #e4e4e4;
    font-size: 20px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: rgba(79, 79, 79, 1);
    cursor: pointer;
  }
  li:hover {
    color: #1ca2a7 !important;
  }
  .active {
    color: #1ca2a7 !important;
    border-bottom: 0px solid #ddd;
  }
}
.point {
  padding-bottom: 10px;
  span {
    float: left;
  }
}
.info {
  position: relative;
  padding-bottom: 16px;
  > span {
    position: absolute;
    left: 0;
    top: 10px;
  }
}
.name {
  font-size: 24px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: rgba(22, 22, 22, 1);
  margin-top: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-bottom: 16px;
}

.list {
  padding-top: 30px;
  img {
    margin-left: 0;
  }
}
.list-li {
  padding-bottom: 30px;
  cursor: pointer;
}
.icon {
  font-size: 18px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: rgba(142, 142, 142, 1);
  padding-top: 16px;
  img {
    margin-right: 10px;
  }
   i{
    font-size: 24px;
  }

}
.el-carousel__container {
  background: rgba(255, 255, 255, 1);
  border: 1px solid rgba(220, 220, 220, 1);
  box-shadow: 1px 2px 10px 0px rgba(153, 153, 153, 0.23);
  border-radius: 10px;
  padding: 20px;
}

.news-list {
  li {
    position: relative;
    text-indent: 20px;
    color: #505050;
    padding: 8px 20px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  li::after {
    display: inline-block;
    content: "";
    position: absolute;
    left: 20px;
    top: 15px;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: rgba(142, 142, 142, 1);
  }
  li:hover {
    color: #ff8a00 !important;
  }
  li:hover::after {
    background: #ff8a00 !important;
  }
}
.total {
  text-align: center;
  padding: 100px 0 50px 0;
}
.zy {
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 120px;
  text-align: center;
  margin-top: 20px;
  background-size: 100% !important;
  span {
    margin: 0 10px;
  }
  span:first-child {
    font-size: 21px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: rgba(255, 255, 255, 1);
  }
  span:last-child {
    font-size: 41px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    font-style: italic;
    color: rgba(255, 255, 255, 1);
  }
}
.state {
  color: #1ca2a7;
}
</style>
<style lang="scss">
ul,
li {
  padding: 0;
  list-style: none;
}

.cont .el-tabs__item.is-active {
  font-size: 22px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: rgba(37, 37, 37, 1);
}
.cont .el-tabs__active-bar {
  background-color: #1ca2a7;
}
.cont .el-tabs__item {
  height: 50px;
  line-height: 50px;
}
.cont .el-tabs__item:hover {
  color: #1ca2a7;
}
.cont .el-tabs__header {
  margin: 0;
}
.info .el-button {
  border: none;
  padding: 0;
  width: 100%;
  line-height: 30px;
  white-space: normal;
  text-align: left;
  text-indent: 45px;
  span {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    font-size: 20px;
    color: #4f4f4f;
  }
}
.info {
  .el-button:hover {
    background: none;
  }
}
.el-carousel__container {
  margin-top: 30px;
  background: rgba(255, 255, 255, 1);
  border: 1px solid rgba(220, 220, 220, 1);
  box-shadow: 1px 2px 10px 0px rgba(153, 153, 153, 0.23);
  border-radius: 10px;
  overflow: hidden;
}

.tabs .el-tabs__item.is-active {
  color: #1ca2a8;
  font-size: 20px;
  border-bottom-color: #fff !important;
}

.tabs .el-tabs__item {
  height: 50px;
  line-height: 50px;
  font-size: 20px;
}
.tabs .el-tabs__header {
  margin: 0;
  border: none;
}
.tabs .el-tabs__nav {
  float: right;
  border-bottom: 1px solid #e4e7ed !important;
}
.tabs .el-tabs--card > .el-tabs__header .el-tabs__item {
  border-bottom: 1px solid #e3e7ed;
}
.total .el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #1ca2a7 !important;
  cursor: default;
}
.total .el-pagination.is-background .el-pager li:not(.disabled):hover {
  color: #1ca2a7;
}
.icon{
  .el-col-7{
    height: 40px;
    display: flex;
    align-items: center;
  }
}
.el-col-8{
  img{
    width: 90%;
  }
}
</style>